<template>
  <div>
    <div class="head-search">
      <div class="input">
        <span class="iconfont icon-sousuoxiao"></span>
        <input type="text" placeholder="搜索菜谱" v-model="keyword" />
      </div>
      <div class="fanhui" @click="signOut">取消</div>
    </div>
    <div class="exhibition" v-show="searchList">
      <ul v-show="keyword">
        <li v-for="(item, index) in secrchs()" :key="index">
          <p @click="jilu(item.id)">{{ item.foodName }}</p>
        </li>
      </ul>
    </div>

    <div class="history-search">
      <div class="hisTitle">
        <div class="hisTitle-l">最近搜索</div>
        <div class="hisTitle-r" @click="qingkong">清空</div>
      </div>
      <div class="lishi">
        <ul>
          <li v-for="(item, index) in jiluList" :key="index" @click="toinput(item)">{{ item }}</li>
        </ul>
      </div>
    </div>
    <div class="find-search">
      <div class="findTitle">搜索发现</div>
      <div class="findBox">
        <ul>
          <li>万圣节</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { recommendData } from "../api/home.js";
export default {
  data() {
    return {
      keyword: "",
      searchList: null,
      jiluList: [],
    };
  },
  methods: {
    signOut() {
      this.$emit("signOut");
      this.$router.push({
        path: "homeView",
      });
    },
    secrchs() {
      return this.searchList.filter((item) =>
        item.foodName.includes(this.keyword)
      );
    },
    jilu(id) {
      this.$router.push({
        path: "detailsView",
        query: {
          id,
          zhuantai: false,
        },
      });

      let index = this.searchList.findIndex((item) => item.id == id);
      this.jiluList.unshift(this.searchList[index].foodName);
      window.localStorage.setItem("jiluList", JSON.stringify(this.jiluList));
      
    },
    qingkong() {
      window.localStorage.removeItem("jiluList");
      this.jiluList = [];
    },
    toinput(item){
      this.keyword=item
    },

  },
  created() {
    if (localStorage.jiluList) {
      this.jiluList = JSON.parse(window.localStorage.getItem("jiluList"));
    }
    recommendData().then((data) => {
      this.searchList = data.recommendLists;
    });
  },
};
</script>

<style lang="scss" scoped>
.head-search {
  height: 60px;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .input {
    height: 40px;
    width: 290px;
    background-color: #f0f0f0;
    display: flex;
    border-radius: 25px;
    span {
      display: block;
      width: 28px;
      height: 100%;
      text-align: center;
      line-height: 40px;
      font-size: 16px;
      color: #999999;
    }
    input {
      margin: 9px 0;
      width: 230px;
      height: 22px;
      border: 0;
      background-color: #f0f0f0;
      outline: none;
    }
  }
}

.exhibition {
  position: relative;
  padding: 0 20px;
  ul {
    width: 90%;
    height: 80vh;
    background-color: white;
    position: absolute;
    top: 0;
    li {
      width: 300px;
      height: 40px;
      line-height: 40px;
    }
  }
}
.history-search {
  margin-top: 25px;
  padding-left: 20px;
  .hisTitle {
    height: 16px;
    display: flex;
    justify-content: space-between;
    .hisTitle-r {
      padding-right: 20px;
      color: blue;
    }
  }
  .lishi {
    width: 100%;
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        padding: 14px;
        margin-right: 10px;
        margin-top: 12px;
        border-radius: 25px;
        background-color: #f6f6f6;
      }
    }
  }
}

.find-search {
  margin-top: 25px;
  padding-left: 20px;
  .findTitle {
    height: 16px;
  }
  .findBox {
    width: 100%;
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        padding: 14px;
        margin-right: 10px;
        margin-top: 12px;
        border-radius: 25px;
        background-color: #f6f6f6;
      }
    }
  }
}
</style>